package com.inossem.allotin.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.allotin.db.CacheBean;
import com.inossem.allotin.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/5/23 0023.
 * 列表页适配器
 */

public class EmergencyListAdapter extends RecyclerView.Adapter<EmergencyListAdapter.ViewHolder> {

    private Activity context;
    private List<CacheBean> list = new ArrayList<>();
    private OnItemClickListener listener;

    public EmergencyListAdapter(Activity context) {
        this.context = context;
    }

    public void addList(List<CacheBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void clearList() {
        this.list.clear();
        notifyDataSetChanged();
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void removeItem(int i) {
        list.remove(i);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_emergency_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (i % 2 == 0) {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_white));
        } else {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_list_bg));
        }
        CacheBean bean = list.get(i);
        viewHolder.mTvNumber.setText(bean.getReceiptCode());
        viewHolder.mTvType.setText(bean.getReceiptTypeName());
        viewHolder.mTvRemark.setText(bean.getRemark());
        viewHolder.mTvFile.setText(bean.getFileName());
        viewHolder.mTvTime.setText(bean.getCreateTime());

        viewHolder.mCbCheck.setChecked(bean.getIsSelect());
        viewHolder.mCbCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                bean.setIsSelect(isChecked);
                boolean isAllCheck = true;
                for (CacheBean bean : list) {
                    if(!bean.getIsSelect()){
                        isAllCheck = false;
                    }
                }
                if (listener != null)
                    listener.onCheck(isAllCheck);
            }
        });
        viewHolder.layout.setOnClickListener(view -> {
            if (listener != null)
                listener.onItemClick(i);
        });

        viewHolder.mTvFile.setOnClickListener(view -> {
            if (listener != null)
                listener.onFileClick(i);
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickListener {
        void onItemClick(int i);

        void onFileClick(int i);

        void onCheck(boolean isCheck);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb_check)
        CheckBox mCbCheck;
        @BindView(R.id.tv_number)
        TextView mTvNumber;
        @BindView(R.id.tv_type)
        TextView mTvType;
        @BindView(R.id.tv_remark)
        TextView mTvRemark;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_file)
        TextView mTvFile;
        @BindView(R.id.layout)
        LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            Utils.setViewLine(itemView);
        }
    }
}
